'use strict';

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

/**
 * Created by huhai on 17/9/6.
 */
require('babel-polyfill');
const puppeteer = require('puppeteer');
let gPDF = (() => {
  var _ref = _asyncToGenerator(function* (hospitalID = 120129, templateId) {
    const browser = yield puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
      dumpio: true
    });

    const page = yield browser.newPage();
    page.setViewport({
      width: 769,
      height: 1080
    });
    yield page.goto(`http://localhost:3000/${hospitalID}?template=${templateId}`);
    // await page.goto(`http://www.baidu.com`)


    function screenshotPDF() {
      return new Promise(resolve => {
        setTimeout(() => {
          // clip-pdf
          var random = Math.random();
          let name = `${hospitalID}的报告${random}.pdf`;
          page.pdf({
            path: name,
            width: "769px",
            height: "1088px",
            printBackground: true
          }).then(() => {
            // console.log('截图完成！',name)
            page.close().then(() => {
              console.log('截图完成！', name);
              resolve(name);
            });
          });
          // clip-pdf
        }, 3000);
      });
    }

    return screenshotPDF();
  });

  return function gPDF() {
    return _ref.apply(this, arguments);
  };
})();

module.exports = {
  gPDF: gPDF
};
